<?php
/**
 * Created by PhpStorm.
 * User: yuchen
 * Date: 16/4/15
 * Time: 下午3:12
 */

namespace App\Http\Controllers;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Auth;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Input;
use PHPExcel;
use PHPExcel_IOFactory;
use DB;
use Hash;
use Illuminate\Support\Str;
use Cache;
use Illuminate\Support\Facades\File;

class QuestionsController extends Controller
{
    //获取下一题
    public function getQuestions(Request $request)
    {
        $askCode = $request->input('askCode');
        $index = $request->input('index');
        $indextmp= $request->input('indextmp');
        $timuAllWithAskCode = Cache::get($askCode.'_timu');
        $expiresTime = Carbon::now()->addMinutes(20);
        if($index < 1 || $index > count($timuAllWithAskCode))
        {
            return response()->json(['state'=>'0','Error'=>'没有更多题目啦!']);
        }else{
            Cache::put($askCode.'_nowIndex',$index,$expiresTime);
            Cache::put($askCode.'_isStart_'.$indextmp,0,$expiresTime);
            return response()->json(['state'=>'1','question'=>$timuAllWithAskCode[$index]]);
        }
    }

    //统计结果
    public function statisticResult(Request $request)
    {
        $askCode = $request->input('askCode');
        $index = $request->input('index');
        $stuall = array();
        $timuall = array();
        $answerall = array();
        $lastAnsCount = 0;
	set_time_limit(0);
	$i = 0;
        //从缓存中取出
        if(Cache::has($askCode.'_stu')) {
            $stuall = Cache::get($askCode . '_stu');
        }else{
      //      $sturall = null;
       }
        if(Cache::has($askCode.'_timu')) {
            $timuall = Cache::get($askCode . '_timu');
        }else{
        //    $timuall = null;
        }
        if(Cache::has($askCode . '_answer_'.$index)) {
            $answerall = Cache::get($askCode . '_answer_'.$index);
        }else{
          //  $answerrall = null;
        }
//	 while(1) {
  //          //从缓存中取出
    //        if (Cache::has($askCode . '_stu')) {
     //           $stuall = Cache::get($askCode . '_stu');
      //      } else {
             //   $sturall = null;
       //     }
       //     if (Cache::has($askCode . '_timu')) {
       //         $timuall = Cache::get($askCode . '_timu');
       //     } else {
             //   $timuall = null;
       //     }
       //     if (Cache::has($askCode . '_answer_' . $index)) {
       //         $answerall = Cache::get($askCode . '_answer_' . $index);
       //     } else {
       //      //   $answerall = null;
       //     }
       //     if (!Cache::has($askCode . '_lastAnswerCount_' . $index)) {
       //         Cache::add($askCode . '_lastAnswerCount_' . $index, count($answerall), Carbon::now()->addMinutes(600));
//		$lastAnsCount = Cache::get($askCode . '_lastAnswerCount'. $index);
//            }else{
//                $lastAnsCount = Cache::get($askCode . '_lastAnswerCount'. $index);
//		echo($lastAnsCount);
  //          }
    //        if(count($answerall) != $lastAnsCount)
      //      {
       //         Cache::add($askCode . '_lastAnswerCount_' . $index, count($answerall), Carbon::now()->addMinutes(600));
         //       break;
          //  }
	 //   usleep(500000);
	 //   $i = $i + 1;
         //   if($i == 40)
	 //   {	
	//	return response()->json(['answerCount'=>-1]);
	//	break;
	  //  }		
       // }

        $answerCount = count($answerall);
        $answerACount = 0;
        $answerBCount = 0;
        $answerCCount = 0;
        $answerDCount = 0;
        $answerOtherCount = 0;
        $correctAnswerCount = 0;

        $otherAnswerArray = array();

        $allAnswerCount = array();
        foreach ($answerall as $answer)
        {
            if(array_key_exists((string)$answer['answer'], $allAnswerCount))
            {
                $allAnswerCount[(string)$answer['answer']] += 1;
            }else{
                $allAnswerCount[(string)$answer['answer']] = 1;
            }
            if($answer['isCorrect'] == "YES")
            {
                $correctAnswerCount++;
            }
        }

        if(count($answerall) > 0) {
            foreach ($answerall as $key => $value) {
                $answertime[$key] = $value['answerTime'];
            }
            $correctRate = number_format($correctAnswerCount/$answerCount * 100, 2);
            array_multisort($answertime,$answerall);
            //var_dump($answerall);
            //var_dump($timuall);
            return response()->json(['answerCount'=>$answerCount,'correctRate'=>$correctRate,'allAnswerCount'=>$allAnswerCount,'fastAnswer'=>$stuall[$answerall[0]['stuid']]]);
        }else{
            return response()->json(['answerCount'=>$answerCount,'correctRate'=>'0','allAnswerCount'=>$allAnswerCount,'fastAnswer'=>'没人答题']);
        }
    }

    //获取当前题目
    public function nowQuestions(Request $request){
        $askCode = $request->input('askCode');
        $index = $request->input('index');
        //$index = Cache::get($askCode.'_nowIndex');
        if(Cache::has($askCode.'_timu')){
            $timuAllWithAskCode = Cache::get($askCode.'_timu');
            if($index < 1 || $index > count($timuAllWithAskCode))
            {
                return response()->json(['state'=>'0','error'=>'没有更多题目啦!','maxNum'=>count($timuAllWithAskCode)]);
            }else{
                return response()->json(['state'=>'1','question'=>$timuAllWithAskCode[$index]]);
            }
        }else{
            return response()->json(['state'=>'2','question'=>$askCode]);
        }
    }

    public function setAskMode(Request $request){

        $mode = $request->input("mode");
        $askCode = $request->input("askCode");
        $expiresTime = Carbon::now()->addMinutes(120);
        $options["mode"] = $mode;
        Cache::put($askCode."_option",$options,$expiresTime);
        $opt = Cache::get($askCode."_option");
    }
}
